import { useRoute, useRouter } from 'vue-router'

import { useTabsStore } from '@/store'
import { type Events, bus } from '@/utils'

export function useBack(event?: keyof Events) {
  const route = useRoute()
  const router = useRouter()
  const tabsStore = useTabsStore()

  function handleCancel() {
    tabsStore.delTab(route).then(() => {
      router.back()

      if (event) {
        bus.emit(event)
      }
    })
  }

  return {
    handleCancel
  }
}
